import requests
from yaml_data.yaml_data import write_yaml


def NewUserEnroll(phone):
    """
    :param phone: 自己登录手机号
    :return: user_id
    """
    code_url = 'http://124.220.33.63:8700/sms/login/send'
    login_url = 'http://124.220.33.63:8700/login/phone'
    header = {
        'Content-Type': 'application/json'
    }
    code_data = {
        "To": phone
    }

    code_res = requests.post(url=code_url, headers=header, json=code_data)
    print(code_res.json())
    code = code_res.json().get('data').get('Code')
    # print(code)
    login_data = {
        "Phone": phone,
        "PhoneCode": code
    }
    login_res = requests.post(url=login_url, headers=header, json=login_data)
    print(login_res.json())
    user_id, token = login_res.json().get('data')[0].get('ID'), login_res.json().get('data')[0].get('Token')

    return token


if __name__ == '__main__':
    path = '../DataTest/yaml_data/NewEnrollPhone.yaml'
    phone_list = ['13101212123', '13101212124']
    for phone in phone_list:
        token = NewUserEnroll(phone)
        write_yaml(path, data=[{'phone': phone, 'token': token}])
